<?php
class fileimages extends Controller
{
	  
	protected $_templates;
	
	function fileimages()
	{
		parent::Controller();
		
		$this->load->model('fileimages_model','fileimages');
		$this->load->helper('img_helper');
		$this->mshop = $this->config->item('mshop');
		$this->pre_message = "";
		//$this->permit_library->check_permit();
		$this->session->set_userdata(array('Url'=>uri_string()));           
	}
	
	// Thống kê toàn bộ gian hàng
	function index()
	{
		$this->listfileimages() ;
	}
	
	/**********fileimages*************/
	function listfileimages()
	{
		$data['title'] =lang('title_fileimages');
		$data['add'] = 'fileimages/addfileimages';
		// list catid
		$data['listcat'] = $this->fileimages->get_list_category() ;
		
		$config['per_page']  	= 30;
		$config['uri_segment'] 	= 4 ; 
		$data['list'] = $this->fileimages->getAllfileimages($config['per_page'],$this->uri->segment('4'));
		//$data['pagination']    = $this->pagination->create_links();          
		$this->_templates['page'] = 'fileimages/fileimages_list'; 
		$this->site_library->load($this->_templates['page'],$data);          
	}
	
	function search()
	{
		$catid = $this->input->post('catid') ;
		$style = $this->input->post('style') ;
		
		$data['title'] = lang('title_search');
		$data['add'] = 'fileimages/addfileimages';
		
		$data['listcat'] = $this->fileimages->get_list_category() ;
		
		$config['per_page']  =   30;
		$config['uri_segment'] = 4; 
		$data['list'] = $this->fileimages->get_search($catid, $style, $config['per_page'], $this->uri->segment('4'));
		$data['pagination']    = $this->pagination->create_links();  
		        
		$this->_templates['page'] = 'fileimages/fileimages_list'; 
		$this->site_library->load($this->_templates['page'],$data);          
	}
	
	function addfileimages()
	{
		$data['title'] = lang('title_addfileimages');
		
		$data['listcat'] = $this->fileimages->get_list_category();
		
		$this->form_validation->set_rules('name','Giá cty','trim|required');
		
		if($this->form_validation->run()){
			if($this->fileimages->savefileimages()){
				$this->session->set_flashdata('message',lang('save_success'));
				redirect('fileimages/listfileimages');
			}else{
				$this->pre_message =lang('save_success');
			}
		}    
		$data['message'] = $this->pre_message;
		$this->_templates['page'] = 'fileimages/fileimages_add';
		$this->site_library->load($this->_templates['page'],$data);
	}
	
	function editfileimages()
	{
		$fileimagesID = $this->uri->segment(3);
		$rs = $this->fileimages->get_info_fileimages($fileimagesID);
		$data['title'] = lang('editfileimages').$rs->title;
		$data['listcat'] = $this->fileimages->get_list_category();
		
		$this->form_validation->set_rules('name','Tên hình ảnh','trim|required');
		
		if($this->form_validation->run())
		{          
			if($this->fileimages->savefileimages())
			{
				$this->session->set_flashdata('message',lang('save_success'));
				redirect('fileimages/listfileimages');
			}
			else
			{
				$this->pre_message =lang('save_fail');
			}    
		}
		
		$data['rs'] = $rs;
		$data['message'] = $this->pre_message;
		$this->_templates['page'] = 'fileimages/fileimages_edit';
		$this->site_library->load($this->_templates['page'],$data);
	} 
	
	function delsfileimages()
	{
		$page = (int)$this->input->post('page');
		$catid = (int)$this->input->post('catid');
	  
		if(isset($_POST['btn_submitHN']) && ($_POST['btn_submitHN']=='OrderHN'))
		{
			$pid = $this->input->post('ar_pid');
			$ar_pid = explode(',',$pid);
			$num_pid =  count($ar_pid);
			for($i=0;$i<$num_pid;$i++){
				$Ordering = (int)$this->input->post('OrderHN'.$ar_pid[$i]);
				if($Ordering!=0)
				{
					$data = array(
						'OrderHN' => $Ordering
					);
					$this->db->where('fileimagesID',$ar_pid[$i]);
					if($this->db->update('fileimages',$data)){
						$this->session->set_flashdata('message',lang('arrangement_product_success'));
					}else{
						$this->session->set_flashdata('message',lang('arrangement_product_success'));
					}
				}
			}
		}
	  if(isset($_POST['btn_submit']) && ($_POST['btn_submit']=='Lưu'))
	  {
		$pid = $this->input->post('ar_pid');
		$ar_pid = explode(',',$pid);
		$num_pid =  count($ar_pid);
		for($i=0;$i<$num_pid;$i++){
			$Ordering = (int)$this->input->post('Order'.$ar_pid[$i]);
			if($Ordering!=0)
			{
				$data = array(
					'Ordering' => $Ordering
				);
				$this->db->where('fileimagesID',$ar_pid[$i]);
				if($this->db->update('fileimages',$data)){
					$this->session->set_flashdata('message',lang('arrangement_success'));
				}else{
					$this->session->set_flashdata('message',lang('arrangement_fail'));
				}
			}
		}
		
	  }else{
		if(!empty($_POST['ar_id']))
		{
	
			$ar_id = $this->input->post('ar_id');
			
			if(!empty($_POST['btn_submit']))
			{
				for($i = 0; $i < sizeof($ar_id); $i ++) {
					if ($ar_id[$i]){
						if($this->fileimages->delfileimages($ar_id[$i]))
							$this->session->set_flashdata('message',lang('del_success'));
						else $this->session->set_flashdata('message',lang('del_fail'));
					}
				}
			}
		}              
	  }
	  redirect('fileimages/listfileimages/'.$catid.'/'.$page); 
	
	}
	
	function delfileimages()
	{
	  $id = $this->uri->segment(3);
	  $catid = $this->uri->segment(4);
	  $page = $this->uri->segment(5);
		if($this->fileimages->delfileimages($id))
			$this->session->set_flashdata('message',lang('del_success'));
		else $this->session->set_flashdata('message',lang('del_fail'));
	  redirect('fileimages/listfileimages/'.$page);          
	}  
	
}
?>
